
Page({
    data: {
      array: [1,2,3,4,5,6],
      index: 0,
      dateStart: '', // 开始时间
      hasProperty: false, // hasProperty===true不需要获取楼盘列表（来自楼盘详情，指定楼盘预约）
      propertyList: [], // 楼盘信息
      propertyListIndex: 0,
      username: '', // 来访人
      people: '', // 来访人数
      chooseDate: '', // 选择日期
      chooseProperty: '', // 选择的楼盘
      remark: '', // 备注
    },
    onLoad(t) {
      // getApp().page.onLoad(this, t);
      let options = {};
      if (t.params) {
        options = {
          propertyList: JSON.parse(t.params),
          chooseProperty: JSON.parse(t.params)[0],
          hasProperty: true
        }
      }
      options.dateStart = this.formatTime(new Date()); // 初始化开始时间
      this.setData({...options});
    },
    onReady() {},
    onShow() {
      this.data.hasProperty ? function() {} : this.getPropertyList({
        "hotSell": 1,
        "pageNum": 1,
        "pageSize": 100,
        "queryStatus": 0
      }, 'new')
    },
    onHide() {},
    onUnload() {},
    onPullDownRefresh() {},
    onReachBottom() {},
    onShareAppMessage() {},
    bindPickerChange(e) {
      let target = e.currentTarget.dataset, params = {};
      switch (target.type) {
        case 'date':
          params.chooseDate = e.detail.value
          break;
        case 'people':
          params.people = this.data.array[e.detail.value] + '人';
          params.index = e.detail.value;
          break;
        case 'property':
          params.chooseProperty = this.data.propertyList[e.detail.value];
          params.propertyListIndex = e.detail.value;
          break;
        case 'username':
          params.username = e.detail.value;
          break;
        case 'remark':
          params.remark = e.detail.value;
          break;
        default:
          break;
      }
      this.setData({ ...params })
    },
    submit() { // 提交
      if (!getApp().globalData.userInfo || !getApp().globalData.userInfo.memberId) {
        this.needLogin({type: 'navigateTo'});
        return false;
      }
      let msg = {
        username: '请输入姓名',
        people: '请选择来访人数',
        chooseDate: '请选择看房时间',
        chooseProperty: '请选择楼盘'
      }
      for (let key in msg) {
        if (!this.data[key]) {
          wx.showToast({
            title: msg[key], //提示的内容,
            icon: 'none', //图标,
            duration: 2000, //延迟时间,
            mask: false, //显示透明蒙层，防止触摸穿透,
            success: res => {}
          });
          return false;
        }
      }
      this.postForm({
        checkingTime: this.data.chooseDate,
        houseId: Number(this.data.chooseProperty.id),
        memberId: Number(getApp().globalData.userInfo.memberId),
        name: this.data.username,
        visitNum: parseInt(this.data.people),
        remark: this.data.remark
      })
    },
    formatTime(date) {
      const year = date.getFullYear()
      const month = date.getMonth() + 1
      const day = date.getDate()
      return [year, month, day].map(this.formatNumber).join('-')
    },
    formatNumber(n) {
      n = n.toString()
      return n[1] ? n : '0' + n
    },
    // -------------------------------
    getPropertyList(params, type) { // 获取楼盘列表
      getApp().api.getHouseList(params).then(res => {
        this.setData({
          propertyList: type === 'new' ? res.data.list || [] : [...this.data.propertyList, ...(res.data.list || [])]
        })
      })
    },
    postForm(params) {
      getApp().api.postReservationForm(params).then(res => {
        if (res.code === 200) {
          wx.navigateTo({ url: '/pages/my-appointment/my-appointment' });
        }
      })
    }
  })
